package com.dingqb.androidproject.listview;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.dingqb.androidproject.MainActivity;
import com.dingqb.androidproject.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dingqb on 2018/4/19.
 */

public class ListViewDemo extends Activity {
    ListView listview;
    static int num = 0;
    MyAdapter baseAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_listview);
        listview = findViewById(R.id.listview);
        final List<String> data = new ArrayList<>();
        for (int i = 0; i < 30; i++) {
            data.add("i==" + num);
            num++;
        }
        baseAdapter = new MyAdapter(data);
        listview.setAdapter(baseAdapter);
        listview.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
                    final List<String> data = new ArrayList<>();
                    for (int i = 0; i < 10; i++) {
                        data.add("i==" + num);
                        num++;
                    }
                    baseAdapter.addData(data);
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

            }
        });
    }

    class MyAdapter extends BaseAdapter {
        List<String> data;

        public MyAdapter(List<String> data) {
            this.data = data;
        }

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int position) {
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView = LayoutInflater.from(ListViewDemo.this).inflate(R.layout.listview_item, null);
            TextView item = convertView.findViewById(R.id.item);
            item.setText(data.get(position) + "");
            return convertView;
        }

        public void addData(List<String> list) {
            this.data.addAll(list);
            notifyDataSetChanged();
        }
    }
}
